<?php

/**
 # com_jvframwork - JV Framework
 # @version		1.5.x
 # ------------------------------------------------------------------------
 # author    Open Source Code Solutions Co
 # copyright Copyright (C) 2011 joomlavi.com. All Rights Reserved.
 # @license - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL or later.
 # Websites: http://www.joomlavi.com
 # Technical Support:  http://www.joomlavi.com/my-tickets.html
-------------------------------------------------------------------------*/
// No direct access
defined('JPATH_BASE') or die;

abstract class JVFactory{
    
    public static $document  = null;
	public static $parameter = null;    
	public static $useragent = null;
    public static $layouts   = null;
    public static $isRTL     = null;
    public static $parser    = null;    
    
    /**
     * Get Instance JVFrameWork
     * 
     * @param  object $document JDocumentHTML object.
     * @access public     
     * @return object JVFrameWork
     */
    public static function getFramework($doc = null, $client='site'){
        if (!self::$document) {
            if($client == 'site'){
                self::$document = JV::getInstance($doc);
            }elseif($client == 'admin'){
                self::$document = JVAdmin::getInstance($doc);
            }			
		}
        
		return self::$document;
    }
    
    /**
     * Get Instance JVLayout
     * 
     * @param $useragent
     * @param $activeTheme
     * @param $isRTL 
     * @access public     
     * @return object JVLayout
     */
    public static function getJVLayout($useragent = null,$activeTheme = null,$isRTL = null, $client = 'site'){
        if (!self::$layouts) {
            $useragent      = $useragent    ? $useragent:self::$useragent;
            $activeTheme    = $activeTheme  ? $activeTheme:self::$document->getActiveTheme();
            $isRTL          = $isRTL        ? $isRTL:self::isRTL(); 
            
            if($client == 'site'){
                self::$layouts =& JVLayout::getInstance($useragent, $activeTheme, $isRTL );
            }elseif($client == 'admin'){
                self::$layouts =& JVAdminLayout::getInstance($useragent, $activeTheme, $isRTL );
            }
		}
		return self::$layouts;
    }
    
    /**
     * Get Instance LayoutParser
     * 
     * @param $mainlayout
     * @param $layout_type_name
     * @access public     
     * @return object JVLayout
     */
    public static function getLayoutParser($mainlayout = null, $layout_type_name = null){
        if (!self::$parser) {
			self::$parser = new JVLayoutParser($mainlayout, $layout_type_name);
		}
		return self::$parser;
    }
    
  
   /** Get Theme Parameter
     * 
     * @access public     
     * @return object JVLayout
     */
    public static function &getParameter(){
        if (!self::$parameter) {
            $activeTheme = self::$document->getActiveTheme(); 
            $db =& JFactory::getDBO();
            $db->setQuery("SELECT params FROM #__jv_themes WHERE id=$activeTheme->id");
            $data   = $db->loadObject();
            $params = $data->params; 
            if($params == 'null' || $params == ''){
                $path = JVPATH_THEME.$activeTheme->theme.DS.'params.json';
                if(JFile::exists($path)){
                    $params = JFile::read($path);
                }
            }
            
            self::$parameter  = new JVParams($params,$activeTheme);
		}
		return self::$parameter;
    }
    
    /**
     * Get user agent
     * 
     * @access public      
     * @return string Current user agent view site.
     */
    public static function getUserAgent() {
        $param   = self::$parameter;
        $browser = new Browser(); // Detect browser and platform.
        
        if ($browser->isBrowser('iPhone')) {
            self::$useragent = 'iphone';
        } elseif ($browser->isBrowser('Android')) {
            self::$useragent = 'android';
        } elseif ($browser->isMobile()) {
            self::$useragent = 'handheld';
        } else {
            self::$useragent = 'standard';
            if ($browser->getBrowser() == 'IE' && $browser->getVersion() == '6') {
                self::$document->enableWarning();
            }
        }
        
        if(isset($_COOKIE['jvtheme'.self::$document->getActiveTheme()->id.'_version'])){
            self::$useragent = $_COOKIE['jvtheme'.self::$document->getActiveTheme()->id.'_version'];
        }
        
        if((int)$param->enableMobileLayout() == 0){
            self::$useragent =  'standard';  
        }     
        
        return self::$useragent;
    }
    
    /**
     * isRTL
     * 
     * @access public      
     * @return string Current user agent view site.
     */
    public static function isRTL() {        
        if(isset($_COOKIE['jvtheme'.self::$document->getActiveTheme()->id]['direction'])){
            self::$isRTL = $_COOKIE['jvtheme'.self::$document->getActiveTheme()->id]['direction'] == 'rtl' ? true:false; 
        } else {
            self::$isRTL = (self::$document->get('Direction') == 'rtl' || self::$parameter->getDirection() == 'rtl') ? true:false;
        }
         
        return self::$isRTL;
    }
    
    
}